package com.tsd.system.service.impl;

import com.tsd.CommonMsgNameConstants;
import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysGroupDutyMapper;
import com.tsd.system.entity.SysGroupDuty;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysGroupDutyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupDuty 服务实现层
 *
 * @author 15971
 * @date 2019/12/27 14:42:27
 */
@Service
public class SysGroupDutyServiceImpl extends BaseServiceImpl implements SysGroupDutyService {

    @Resource
    private SysGroupDutyMapper sysGroupDutyMapper;

    @Override
    public SysGroupDuty getSysGroupDutyById(Long id) throws Exception {
        return sysGroupDutyMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysGroupDuty queryBySid(String sid) throws Exception {
        return sysGroupDutyMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysGroupDuty(Long id, SysUser opUser) throws Exception {
        SysGroupDuty old = sysGroupDutyMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        sysGroupDutyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveSysGroupDuty(SysGroupDuty record, SysUser opUser) throws Exception {
        super.checkEmpty(record.getName(), "缺少名称");
        SysGroupDuty old = sysGroupDutyMapper.selectOtherByCodeInSameParent(record);
        super.checkNotEmpty(old, "同一节点下已有该编号的数据");
        if (record.getId() != null) {
            old = getSysGroupDutyById(record.getId());
        } else {
            old = queryBySid(record.getSid());
        }
        setFullCode(record);
        if (old != null) {//这样可以支持前端只是传递了id或者sid
            record.setId(old.getId());
            record.setSid(old.getSid());
        }
        if (HlpUtils.isEmpty(record.getId())) {
//            SysGroupDuty old2 = sysGroupDutyMapper.selectOtherByNameInSameParent(record);
//            super.checkNotEmpty(old2, "同一节点下已有该名称的数据");
            checkDutyName(record);
            if (record.getSn() == null) {
                record.setSn(1);
            }
            record.setState(1);
            record.setSid(super.getPrimaryKey());
            record.setCreate_date(new Date());
            record.setDr(Constants.DR_VALID);
            record.setCreator_name(opUser.getName());
            record.setCreator_sid(opUser.getSid());
            sysGroupDutyMapper.insertSelective(record);
        } else {
//            SysGroupDuty duty = getSysGroupDutyById(record.getId());
//            if (record.getName() != null && !record.getName().equals(duty.getName())) {
//                SysGroupDuty checkName = sysGroupDutyMapper.selectByName(record.getName());
//                if (checkName != null) {
//                    throw new HlpException("已存在该名称的数据");
//                }
//            }
            checkDutyName(record);
            record.setUpdate_date(new Date());
            record.setUpdator_name(opUser.getName());
            record.setUpdator_sid(opUser.getSid());
            sysGroupDutyMapper.updateByPrimaryKeySelective(record);
        }
    }


    private void checkDutyName(SysGroupDuty record) throws Exception {
        List<SysGroupDuty> sysGroupDuties = sysGroupDutyMapper.selectByGroupSid(record.getGroup_sid());
        for (SysGroupDuty sysGroupDuty : sysGroupDuties) {
            if (sysGroupDuty.getName().equals(record.getName())) {
                throw new HlpException("已存在该名称的数据");
            }
        }
    }

    @Override
    public List<SysGroupDuty> findSysGroupDutys(Map<String, Object> params, PageBean page) throws Exception {
        return sysGroupDutyMapper.selectByPage(params, page);
    }

    @Override
    public List<Map<String, Object>> findSysGroupDuty(Map<String, Object> params, PageBean page) throws Exception {
        return sysGroupDutyMapper.selectParamsByPage(params, page);
    }

    @Override
    public List<SysGroupDuty> findSysGroupDutys(Map<String, Object> params) throws Exception {
        return sysGroupDutyMapper.selectByParams(params);
    }

    @Override
    public void batchDeleteDuty(List<String> sids, SysUser opUser) throws Exception {
        for (String sid : sids) {
            SysGroupDuty old = sysGroupDutyMapper.selectBySid(sid);
            super.checkEmpty(old, CommonMsgNameConstants.ERROR_MSG_RECORD_NOT_EXISTS);
            super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
            sysGroupDutyMapper.deleteByPrimaryKey(old.getId());
        }
    }

    /**
     * 设置资源完整编码
     *
     * @param record
     * @throws HlpException
     */
    private void setFullCode(SysGroupDuty record) throws HlpException {
        String parentId = record.getParent_sid();
        if (HlpUtils.isEmpty(parentId) || com.tsd.core.constants.Constants.ROOT_PARENT_SID.equals(parentId)) {
            record.setFull_code(record.getId() + ".");
        } else {
            SysGroupDuty parent = sysGroupDutyMapper.selectBySid(parentId);
            if (parent == null) {
                throw new HlpException("上级单位不存在，上级单位的sid：" + parentId);
            } else {
                record.setFull_code(parent.getFull_code() + record.getId() + ".");
            }
        }
    }

}
